﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using EFERP;
using HRM.DICTIONARY.DAL;
using DevExpress.XtraEditors.Controls;

namespace HRM.DICTIONARY.UI
{
    public partial class xfrmSYSUSERAdd : DevExpress.XtraEditors.XtraForm
    {
        sys_user obj = null;
        ICommon<sys_user> exeDic;
        CloseOrNew CloseNew = CloseOrNew.Close;

        ICommon<sys_group> exeSysGroup;
        ICommon<hrm_branch> exeHrmBranch;
        ICommon<hrm_department> exeHrmDepartment;
        ICommon<hrm_group> exeHrmGroup;
        ICommon<hrm_employee> exeHrmEmployee;

        public xfrmSYSUSERAdd(ICommon<sys_user> _exeDic)
        {
            InitializeComponent();
            exeDic = _exeDic;
            exeDic.myAction = Actions.Insert;
            this.obj = new sys_user();
            SetDataIntoUI();
            //Add if has gridlookup from another table
            LoadInitGridLookup();
            SetDataIntoGridLookup();
        }
        public xfrmSYSUSERAdd(sys_user currObj, ICommon<sys_user> _exeDic)
        {
            InitializeComponent();
            exeDic = _exeDic;
            exeDic.myAction = Actions.Update;
            this.obj = currObj;
            SetDataIntoUI();
            //Add if has gridlookup from another table
            LoadInitGridLookup();
            SetDataIntoGridLookup();
        }

        private void LoadInitGridLookup()
        {
            exeSysGroup = new Common<sys_group>(StaticVariable.ERPEn);
            exeHrmBranch = new Common<hrm_branch>(StaticVariable.ERPEn);
            exeHrmDepartment = new Common<hrm_department>(StaticVariable.ERPEn);
            exeHrmGroup = new Common<hrm_group>(StaticVariable.ERPEn);
            exeHrmEmployee = new Common<hrm_employee>(StaticVariable.ERPEn);
        }

        private void xfrmSYSUSERAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            TotalClose();
        }

        private void SetDataIntoUI()
        {
            if (obj == null)
                return;
            else
            {
                switch (exeDic.myAction)
                {
                    case Actions.Update:
                        txtTenNguoiDung.Text = obj.username.ToString();
                        txtMatKhau.Text = obj.password.ToString();
                        txtNhapLaiMatKhau.Text = obj.password.ToString();
                        txtMoTa.Text = obj.description.ToString();
                        glkSysGroup.EditValue = obj.sys_group.group_name;
                        glkNhanVien.EditValue = obj.employeecode;
                        glkBranch.EditValue = obj.branchcode;
                        glkDepartment.EditValue = obj.departmentcode;
                        glkGroupCode.EditValue = obj.groupcode;
                        txtEmail.Text = obj.email;
                        txtMoTa.Text = obj.description;
                        cbbQuanLy.SelectedIndex = Convert.ToInt32(obj.management);
                        cbKichHoat.Checked = Convert.ToBoolean(obj.active);
                        btnLuuDong.Visible = true;
                        btnThoat.Visible = true;
                        btnLuuThem.Visible = false;
                        break;
                    case Actions.Insert:
                        obj = new sys_user();
                        txtTenNguoiDung.Text = "";
                        txtMatKhau.Text = "";
                        txtNhapLaiMatKhau.Text = "";
                        txtMoTa.Text = "";
                        glkSysGroup.EditValue = "";
                        glkNhanVien.EditValue = "";
                        glkBranch.EditValue = "";
                        glkDepartment.EditValue = "";
                        glkGroupCode.EditValue = "";
                        txtEmail.Text = "";
                        txtMoTa.Text = "";
                        cbbQuanLy.SelectedText = "Tất Cả";
                        cbKichHoat.Checked = Convert.ToBoolean(obj.active);
                        btnLuuDong.Visible = true;
                        btnThoat.Visible = true;
                        btnLuuThem.Visible = true;
                        break;
                    case Actions.InsertMultiple:
                        obj = new sys_user();
                        txtTenNguoiDung.Text = "";
                        txtMatKhau.Text = "";
                        txtNhapLaiMatKhau.Text = "";
                        txtMoTa.Text = "";
                        glkSysGroup.SelectedText = "";
                        glkNhanVien.SelectedText = "";
                        glkBranch.EditValue = "";
                        glkDepartment.EditValue = "";
                        glkGroupCode.EditValue = "";
                        txtEmail.Text = "";
                        txtMoTa.Text = "";
                        cbbQuanLy.SelectedText = "Tất Cả";
                        btnLuuDong.Visible = true;
                        btnThoat.Visible = true;
                        btnLuuThem.Visible = true;
                        break;
                }
            }
        }
        private bool SetUIIntoData()
        {
            if (txtMatKhau.Text == "" || txtTenNguoiDung.Text == "" || cbbQuanLy.Text == "")
            {
                XtraMessageBox.Show("Vui lòng không để trống thông tin mã chức vụ hoặc tên chức vụ");
                exeDic.myAction = Actions.View;
                return false;
            }
            obj.username = txtTenNguoiDung.Text;
            if (txtMatKhau.Text != txtMatKhau.Text)
            {
                XtraMessageBox.Show("Mật khẩu của bạn nhập không đúng");
                return false;
            }
            obj.password = txtMatKhau.Text;
            obj.description = txtMoTa.Text;
            obj.group_id = glkSysGroup.EditValue.ToString();
            obj.employeecode = glkNhanVien.EditValue.ToString();
            obj.email = txtEmail.Text;
            obj.description = txtMoTa.Text;
            obj.management = cbbQuanLy.SelectedIndex;
            obj.branchcode = glkBranch.EditValue.ToString();
            obj.departmentcode = glkDepartment.EditValue.ToString();
            obj.groupcode = glkDepartment.EditValue.ToString();
            obj.employeecode = glkNhanVien.EditValue.ToString();
            cbKichHoat.Checked = Convert.ToBoolean(obj.active);
            return true;
        }
        private void TotalControl()
        {
            bool KQ = false;
            switch (exeDic.myAction)
            {
                case Actions.Insert:
                    KQ = SetUIIntoData();
                    if (KQ)
                    {
                        KQ = exeDic.InsertOnGridView(obj);
                        if (KQ)
                        {
                            exeDic.myAction = Actions.Insert;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;

                case Actions.Update:
                    KQ = SetUIIntoData();
                    if (KQ)
                    {
                        KQ = exeDic.UpdateOnGridView(obj);
                        if (KQ)
                        {
                            exeDic.myAction = Actions.Update;
                            this.Close();
                        }
                        else
                        {
                            exeDic.ValueOfGridViewRow = null;
                            MessageBox.Show("Lỗi cập nhật dữ liệu vào hệ thống");
                        }
                    }
                    break;
                case Actions.InsertMultiple:
                    KQ = SetUIIntoData();
                    if (KQ)
                    {
                        KQ = exeDic.InsertOnGridView(obj);
                        if (KQ)
                        {
                            exeDic.myAction = Actions.InsertMultiple;
                            SetDataIntoUI();
                            if (CloseNew == CloseOrNew.Close)
                                this.Close();
                            else
                                CloseNew = CloseOrNew.New;
                        }
                        else
                        {
                            MessageBox.Show("Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;
            }
        }
        private void TotalClose()
        {
            //exeDic.myAction = Actions.View;
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            TotalClose();
        }
        private void btnLuuDong_Click(object sender, EventArgs e)
        {
            CloseNew = CloseOrNew.Close;
            TotalControl();
        }
        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            exeDic.myAction = Actions.InsertMultiple;
            CloseNew = CloseOrNew.New;
            TotalControl();
        }

        //Add them if we have to combine the other tables to get data
        private bool SetDataIntoGridLookup()
        {
            bool KQ = false;
            KQ = SetDataSysGroupIntoGridLookup();
            if (!KQ)
            {
                XtraMessageBox.Show(exeSysGroup.ErrMsg, "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return KQ;
            }
            KQ = SetDataHrmBranchIntoGridLookup();
            if (!KQ)
            {
                XtraMessageBox.Show(exeHrmBranch.ErrMsg, "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return KQ;
            }
            KQ = SetDataHrmDepartmentIntoUIGridLookup();
            if (!KQ)
            {
                XtraMessageBox.Show(exeHrmDepartment.ErrMsg, "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return KQ;
            }
            KQ = SetDataHrmGroupIntoUIGridLookup();
            if (!KQ)
            {
                XtraMessageBox.Show(exeHrmGroup.ErrMsg, "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return KQ;
            }
            KQ = SetDataHrmEmployeeIntoUIGridLookup();
            if (!KQ)
            {
                XtraMessageBox.Show(exeHrmEmployee.ErrMsg, "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return KQ;
            }
            return KQ;
        }

        private bool SetDataSysGroupIntoGridLookup()
        {
            bool KQ = false;
            KQ = exeSysGroup.AddGridLookupEdit(glkSysGroup, "group_name", "group_id");
            if (KQ)
            {
                if (glkSysGroup.Properties.DataSource != null)
                {
                    glkSysGroup.EditValue = "admiN";
                }
            }
            return KQ;
        }
        //xem lai cho nay glkBranch
        private bool SetDataHrmBranchIntoGridLookup()
        {
            bool KQ = false;
            KQ = exeHrmBranch.AddGridLookupEdit(glkBranch, "branchcode", "branchname");
            return KQ;
        }
        //xem lai cho nay glkBranch
        private bool SetDataHrmDepartmentIntoUIGridLookup()
        {
            bool KQ = false;
            KQ = exeHrmDepartment.AddGridLookupEdit(glkBranch, "branchcode", "branchname");
            return KQ;
        }
        //xem lai cho nay glkBranch
        private bool SetDataHrmGroupIntoUIGridLookup()
        {
            bool KQ = false;
            KQ = exeHrmGroup.AddGridLookupEdit(glkBranch, "branchcode", "branchname");
            return KQ;
        }
        //xem lai cho nay glkBranch
        private bool SetDataHrmEmployeeIntoUIGridLookup()
        {
            bool KQ = false;
            KQ = exeHrmEmployee.AddGridLookupEdit(glkBranch, "branchcode", "branchname");
            return KQ;
        }

        private void cbbQuanLy_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cbbQuanLySelect();
        }
        private void cbbQuanLySelect()
        {
            if (this.cbbQuanLy.SelectedIndex == 0)
            {
                this.glkGroupCode.EditValue = null;
                this.glkDepartment.EditValue = null;
                this.glkBranch.EditValue = null;
                this.lbGroup.Visible = false;
                this.glkGroupCode.Visible = false;
                this.lbDepartment.Visible = false;
                this.glkDepartment.Visible = false;
                this.lbBranch.Visible = false;
                this.glkBranch.Visible = false;
            }
            else if (this.cbbQuanLy.SelectedIndex == 1)
            {
                this.glkGroupCode.EditValue = null;
                this.glkDepartment.EditValue = null;
                this.glkBranch.EditValue = null;
                this.lbGroup.Visible = false;
                this.glkGroupCode.Visible = false;
                this.lbDepartment.Visible = false;
                this.glkDepartment.Visible = false;
                this.lbBranch.Visible = true;
                this.glkBranch.Visible = true;
            }
            else if (this.cbbQuanLy.SelectedIndex == 2)
            {
                this.glkGroupCode.EditValue = null;
                this.glkDepartment.EditValue = null;
                this.glkBranch.EditValue = null;
                this.lbGroup.Visible = false;
                this.glkGroupCode.Visible = false;
                this.lbDepartment.Visible = true;
                this.glkDepartment.Visible = true;
                this.lbBranch.Visible = true;
                this.glkBranch.Visible = true;
            }
            else
            {
                this.glkGroupCode.EditValue = null;
                this.glkDepartment.EditValue = null;
                this.glkBranch.EditValue = null;
                this.lbGroup.Visible = true;
                this.glkGroupCode.Visible = true;
                this.lbDepartment.Visible = true;
                this.glkDepartment.Visible = true;
                this.lbBranch.Visible = true;
                this.glkBranch.Visible = true;
            }
        }

        private void glkSysGroup_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == ButtonPredefines.Plus)
            {
                //xucSysGroupAdd add = new xucSysGroupAdd();
                //add.ShowDialog();
            }
        }
        private void glkBranch_EditValueChanged(object sender, EventArgs e)
        {
            if (this.glkBranch.EditValue != null)
            {
                string branchcode = glkBranch.EditValue.ToString();
                var list = exeHrmDepartment.DbSet.Where(n => n.branchcode == branchcode).ToList();
                glkDepartment.Properties.DataSource = list;
                glkDepartment.Properties.DisplayMember = "departmentname";
                glkDepartment.Properties.ValueMember = "departmentcode";
            }
        }
        private void glkDepartment_EditValueChanged(object sender, EventArgs e)
        {
            if (this.glkDepartment.EditValue != null)
            {
                string departmentcode = glkDepartment.EditValue.ToString();
                var list = exeHrmGroup.DbSet.Where(n => n.departmentcode == departmentcode).ToList();
                glkSysGroup.Properties.DataSource = list;
                glkSysGroup.Properties.DisplayMember = "groupname";
                glkSysGroup.Properties.ValueMember = "groupcode";
            }
            if (glkSysGroup.Text == "")
            {
                glkSysGroup.EditValue = null;
            }
        }
        private void glkNhanVien_EditValueChanged(object sender, EventArgs e)
        {

            if ((this.glkNhanVien.EditValue == null) || (this.glkNhanVien.EditValue.ToString() == ""))
            {
                txtHoLotTen.EditValue = null;
            }
            else
            {
                string employeecode = glkNhanVien.EditValue.ToString();
                var list = exeHrmEmployee.DbSet.Where(n => n.employeecode == employeecode).ToList();
                txtHoLotTen.Text = list[0].firstname.ToString() + " " + list[0].lastname.ToString();
            }
        }
        private void glkNhanVien_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == ButtonPredefines.Delete)
            {
                this.glkNhanVien.EditValue = null;
            }
        }
    }
}